package student.aistar.mapper;

import org.apache.ibatis.annotations.*;
import student.aistar.model.entity.BoxOrder;
import student.aistar.model.entity.BoxSeckill;
import student.aistar.model.entity.Orderdetail;
import student.aistar.model.entity.Orders;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Auther: zhugefeng
 * @Date:2021/10/14 11:03
 **/
public interface BoxSecKillMapper {
    @Select("select * from box_seckill where startTime>now() and status='0'")
    List<BoxSeckill> findAllBoxSeckill();
    /*
     *功能描述:查询所有秒杀表信息
     * @date 2021/10/14
     * @param
     * @return student.aistar.model.entity.BoxSeckill
     */
    @Select("select * from box_seckill where startTime>now() and status='0'")
    List<Map<String,Object>> findAllBoxSeckillBefore();

    /*
     *功能描述:查询所有订单表信息
     * @date 2021/10/14
     * @param
     * @return student.aistar.model.entity.BoxOrder
     */
    @Select("select * from box_order")
    List<BoxOrder> findAllBoxOrder();

    /*
     *功能描述:秒杀活动尚未开始-秒杀活动进行中
     *状态{0} 更新成{1}
     * @date 2021/10/14
     * @param
     * @return void
     */
    @Update("update box_seckill set status='1' where startTime<now() and now()<endTime and status='0'")
    void updateBoxState();
    
    /*
     *功能描述:加载秒杀活动进行中的秒杀id
     * @date 2021/10/14
     * @param         
     * @return java.util.List<student.aistar.model.entity.BoxSeckill>
     */
    @Select("select id from box_seckill where startTime<now() and now()<endTime and status='1'")
    List<Long> findBoxById();
    
    /*
     *功能描述:秒杀活动结束
     * 状态{1}-{2}
     * @date 2021/10/14
     * @param
     * @return void
     */
    @Update("update box_seckill set status='2' where endTime<now() and status='1'")
    void endBoxActive();

    /**
     * 加载出秒杀活动结束之后的商品的id
     */
    @Select("select id from box_seckill where now()>endTime and status='2'")
    List<Long> findBoxEndId();

    /*
     *功能描述: 秒杀订单
     * @date 2021/10/16
     * @param orderNo 订单编号
     * @param uName
     * @param secId
     * @return void
     */
    @Insert("insert into box_order(orderNo,uName,secId) values(#{orderNo},#{uName},#{secId})")
    void saveBoxSeckillOrder(@Param("orderNo") String orderNo,@Param("uName") String uName,@Param("secId") long secId);

    /*
     *功能描述:将秒杀订单插入用户订单表
     * @date 2021/10/16
     * @param order
     * @return int
     */
    @Options(useGeneratedKeys = true, keyProperty = "orderId", keyColumn = "order_id")
    @Insert("insert into orders values(null,#{orderNumber}, #{userId}, 1, 1, 0, 0, 2, '个人', 1, now(), #{orderAmount}, #{payAmount}, 1, 1)")
    int insertOrders(Orders order);

    /*
     *功能描述:将秒杀订单插入订单详情表
     * @date 2021/10/17
     * @param orderId
     * @return void
     */
    @Insert("insert into orderdetail values(null, #{orderId}, #{productId}, #{name}, #{picImg}, 1, #{productSpecName}, #{price}, 1, #{productAmount}, 0)")
    void insertOrderDetail(Orderdetail orderdetail);

    @Select("select * from box_seckill where id = #{killId}")
    BoxSeckill selectBoxSeckillById(Long killId);
}
